package dome6;

/**
 *  浅拷贝：
 *  要想实现克隆这个类必须具备克隆的条件：让这个类实现Cloneable接口，并重写克隆方法
 */

class Money {
    public double money = 12.56;
}

class Student implements Cloneable{
    public String name;
    public Money m = new Money();//组合

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException { // 浅拷贝
        return super.clone();
    }
}




public class Test {

    public static void main(String[] args) throws CloneNotSupportedException{
        Student student1 = new Student();
        Student student2 = (Student) student1.clone();
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
        System.out.println("=================");
        student2.m.money = 99;
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
    }

    public static void main1(String[] args) throws CloneNotSupportedException {
        Student student1 = new Student();
        student1.name = "zhangsan";
        Student student2 = (Student) student1.clone();//向下转型，因为Clone方法的返回值类型是一个Object类型，是所有类的父类
        System.out.println(student1);
        System.out.println(student2);
    }
}
